package com.xbell.app.sys.common;

import java.sql.Clob;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import com.alibaba.fastjson.JSON;

/**
 * map集合操作工具类
 *
 * @Author : HL
 * @create 2023/4/23 11:13
 */


public class Trans {

    /**
     * null 转化为空字符串
     *
     * @param o
     * @return
     */
    public static String ntn(Object o) {
        if (o == null) {
            return "";
        } else {
            return o + "";
        }
    }

    /**
     * 将map的key转化成全小写
     *
     * @param orgMap 目标map
     * @return 转化后的map
     */
    public static Map<String, Object> transformLowerCase(Map<String, Object> orgMap) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        if (orgMap == null || orgMap.isEmpty()) {
            return resultMap;
        }
        Set<String> keySet = orgMap.keySet();
        for (String key : keySet) {
            String newKey = key.toLowerCase();
            if("id".equals(newKey)){  //教学任务表   clob 转成字符串
                Object object = orgMap.get(key);

                if(object instanceof Clob){
                    Clob clob = (Clob)object;
                    try {
                        resultMap.put(newKey, clob.getSubString((long)1, (int) clob.length()));
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }else {
                    resultMap.put(newKey, orgMap.get(key));
                }

            }else {
                resultMap.put(newKey, orgMap.get(key));
            }
        }
        return resultMap;
    }

    /**
     * 将list中的map的key转化成全小写
     *
     * @param orgList 目标list
     * @return 转化后的list
     */
    public static List<Map<String, Object>> transformLowerCase(List<Map<String, Object>> orgList) {
        List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();
        if (orgList!=null&&orgList.size()>0) {
            for (Map<String, Object> orgMap : orgList) {
                resultList.add(transformLowerCase(orgMap));
            }
        }
        return resultList;
    }

    /**
     * 快捷获取参数
     * @author 王文胜
     * @date 2022年1月13日 - 下午1:23:31
     * @copyright 合肥讯百软件科技有限公司
     * @param request
     * @param arrays
     * @return
     */
    public static Map<String, Object> transRequest(HttpServletRequest request, String... arrays) {
        Map<String, String[]> parameterMap = request.getParameterMap();
        List<String> list = Arrays.asList(arrays);
        Map<String, Object> map = new HashMap<String, Object>();
        Set<String> strings = parameterMap.keySet();
        for (String s : strings) {
            Object[] objects = parameterMap.get(s);
            for (int i = 0; i < objects.length; i++) {
                if (objects[i] instanceof String) {
                    objects[i] = ((String) objects[i]).trim();
                    if ("".equals(objects[i])) {
                        objects[i] = null;
                    }
                }
            }
            boolean isNull = true;
            for (Object object : objects) {
                if (object != null) {
                    isNull = false;
                    break;
                }
            }
            if (isNull) {
                objects = null;
            }
            if (list.contains(s)) {
                if (objects != null) {
                    List<Object> objectList = new ArrayList<Object>(Arrays.asList(objects));
                    objectList.remove(null);
                    objects = objectList.toArray(new String[0]);
                }

                map.put(s, objects);
            } else {
                if (isNull) {
                    map.put(s, null);
                } else {
                    map.put(s, objects[0]);
                }
            }
        }
        return map;
    }

    private static String toEntityCase(String key) {
        char[] chars = key.toLowerCase().toCharArray();
        for (int i = 0; i < chars.length; i++) {
            if (Objects.equals(chars[i], '_')) {
                if (i + 1 < chars.length) {
                    chars[i] = '\n';
                    chars[i + 1] = String.valueOf(chars[i + 1]).toUpperCase().charAt(0);
                }
            }
        }
        return String.valueOf(chars).replace("\n","");
    }

    private static Map<String, Object> toEntityMap(Map<String, Object> map) {
        if (map == null) {
            return null;
        }
        Set<String> keySet = map.keySet();
        Map<String, Object> result = new HashMap<>();
        for (String key : keySet) {
            result.put(toEntityCase(key), map.get(key));
        }
        return result;
    }

    public static <T> T toEntity(Map<String, Object> map, Class<T> tClass) {
        if (map == null) {
            return null;
        }
        return JSON.parseObject(JSON.toJSONString(toEntityMap(map)), tClass);
    }
    public static <T> List<T> toEntity(List<Map<String, Object>> list, Class<T> tClass) {
        if (list == null) {
            return null;
        }
        List<T> resultList = new ArrayList<>(list.size());
        for (Map<String, Object> map : list) {
            resultList.add(JSON.parseObject(JSON.toJSONString(toEntityMap(map)), tClass));
        }
        return resultList;
    }
}
